home *** CD-ROM | disk | FTP | other *** search
- /* FileM.c */
- /* Created 3/13/4 1:08 PM by AppMaker */
-
-
- #include "Globals.h"
- #include "ResourceDefs.h"
- #include "Dispatcher.h"
- #include "Miscellany.h"
-
- #include "FileM.h"
-
- #define NIL 0L
-
- #define dialogTop 75
- #define dialogLeft 85
-
- short numOpenTypes;
- SFTypeList openTypeList;
-
- /*----------*/
- void InitFileM ()
- {
- numOpenTypes = 1;
- openTypeList [0] = 'TEXT';
- } /*InitFileM*/
-
- /*----------*/
- Boolean OkToOpen (fType)
- OSType fType;
- {
- short i;
- enum {searching, found, notFound}
- status;
-
- i = 0;
- status = searching;
- while (status == searching) {
- if (i >= numOpenTypes) {
- status = notFound;
- } else {
- if (fType == openTypeList [i]) {
- status = found;
- } else {
- i++;
- }
- }
- } /*while*/
- return (status == found);
- } /*OkToOpen*/
-
- /*----------*/
- static Boolean OpenAppFile (short vRefNum,
- Str255 fName,
- short *fRefNum);
- static Boolean OpenAppFile (vRefNum, fName, fRefNum)
- short vRefNum;
- Str255 fName;
- short *fRefNum;
- {
- Boolean okay;
-
- /* application-specific Open file */
- okay = CheckOS (FSOpen (fName, vRefNum, fRefNum));
- return (okay);
- } /*OpenAppFile*/
-
- /*----------*/
- static void CloseAppFile (short fRefNum);
- static void CloseAppFile (fRefNum)
- short fRefNum;
- {
- Boolean okay;
-
- /* application-specific Close file */
- if (cur->windowKind == 1) { /*1st or only window in set*/
- okay = CheckOS (FSClose (fRefNum));
- }
- } /*CloseAppFile*/
-
- /*----------*/
- static void SaveAppFile (short fRefNum);
- static void SaveAppFile (fRefNum)
- short fRefNum;
- {
- /* application-specific Save file */
- cur->dirty = false;
- } /*SaveAppFile*/
-
- /*----------*/
- static Boolean ReadAppFile (short fRefNum);
- static Boolean ReadAppFile (fRefNum)
- short fRefNum;
- {
- /* application-specific Read file */
- return (false);
- } /*ReadAppFile*/
-
- /*----------*/
- static void DoNew (void);
- static void DoNew ()
- {
- Str255 untitled;
-
- BlockMove (&(**GetString (UntitledID)), untitled, 256);
- OpenWindows (untitled, 0, 0);
- } /*DoNew*/
-
- /*----------*/
- void OpenFile (fileName, vRefNum)
- Str255 fileName;
- short vRefNum;
- {
- short fRefNum;
-
- if (OpenAppFile (vRefNum, fileName, &fRefNum)) {
- OpenWindows (fileName, vRefNum, fRefNum);
- }
- } /*OpenFile*/
-
- /*----------*/
- static void DoOpen (void);
- static void DoOpen ()
- {
- Point dialogOrigin;
- SFReply sfInfo;
-
- SetPt (&dialogOrigin, dialogLeft, dialogTop);
- SFGetFile (dialogOrigin, "", nil, numOpenTypes, &openTypeList, nil, &sfInfo);
- if (sfInfo.good) {
- OpenFile (sfInfo.fName, sfInfo.vRefNum);
- }
- } /*DoOpen*/
-
- /*----------*/
- void Open0Files ()
- {
- DoNew ();
- } /*Open0Files*/
-
- /*----------*/
- static void DoSaveAs (void);
- static void DoSaveAs ()
- {
- SFReply sfInfo;
- short fRefNum;
- StringHandle prompt;
- Str255 suggestion;
- StringHandle untitled;
-
- prompt = GetString (SaveAsPromptID);
- suggestion [0] = 0;
-
- if (CreateFile (&sfInfo, *prompt, suggestion, 'XXXX', 'TEXT')) {
- if (cur->fileNum != 0) {
- CloseAppFile (cur->fileNum);
- }
- if (OpenAppFile (sfInfo.vRefNum, sfInfo.fName, &fRefNum)) {
- SetWTitle (curWindow, sfInfo.fName);
- cur->fileNum = fRefNum;
- cur->volNum = sfInfo.vRefNum;
- SaveAppFile (cur->fileNum);
- } else { /*should never happen*/
- untitled = GetString (UntitledID);
- SetWTitle (curWindow, *untitled);
- cur->fileNum = 0;
- cur->volNum = 0;
- }
- }
- } /*DoSaveAs*/
-
- /*----------*/
- static void DoSave (void);
- static void DoSave ()
- {
- if (cur->fileNum == 0) {
- DoSaveAs ();
- } else {
- SaveAppFile (cur->fileNum);
- }
- } /*DoSave*/
-
- /*----------*/
- static void CloseAppWindow (void);
- static void CloseAppWindow ()
- {
- enum {saveItem = 1, cancelItem, discardItem};
-
- Str255 curTitle;
- short itemNum;
- Boolean okay;
-
- okay = true;
- SetInfo (FrontWindow ());
- if (cur->dirty) {
- GetWTitle (curWindow, curTitle);
- ParamText (curTitle, "", "", "");
- InitCursor ();
- itemNum = Alert (SaveID, nil);
- switch (itemNum) {
- case saveItem:
- DoSave ();
- okay = !errorFlag;
- break;
- case discardItem:
- /*Do nothing*/;
- break;
- case cancelItem:
- errorFlag = true;
- okay = false;
- break;
- } /*switch*/
- }
- if (okay) {
- if (cur->fileNum != 0) {
- CloseAppFile (cur->fileNum);
- }
- CloseCurWindow ();
- }
- } /*CloseAppWindow*/
-
- /*----------*/
- void DoClose ()
- {
- WindowPeek frontPeek;
-
- errorFlag = false;
-
- frontPeek = (WindowPeek) FrontWindow ();
- if (frontPeek->windowKind < 0) {
- CloseDeskAcc (frontPeek->windowKind);
- } else if (frontPeek->windowKind == dialogKind) {
- CloseModelessDialog (FrontWindow ());
- } else {
- CloseAppWindow ();
- }
- } /*DoClose*/
-
- /*----------*/
- void DoQuit (void);
- void DoQuit ()
- {
- Boolean quitting;
-
- quitting = true;
- while (quitting && (FrontWindow () != nil)) {
- SystemTask ();
- DoClose ();
- if (errorFlag) {
- quitting = false;
- }
- } /*while*/
-
- if (quitting) {
- quittingTime = true;
- }
- } /*DoQuit*/
-
- /*----------*/
- static void DoRevert (void);
- static void DoRevert ()
- {
- Str255 fileName;
- Boolean okay;
-
- okay = true;
- if (cur->dirty) {
- GetWTitle (curWindow, fileName);
- ParamText (fileName, "", "", "");
- okay = Confirm (RevertID);
- }
- if (okay) {
- okay = ReadAppFile (cur->fileNum);
- }
- if (okay) {
- InvalRect (&curWindow->portRect);
- }
- } /*DoRevert*/
-
- /*----------*/
- static void DoPageSetup (void);
- static void DoPageSetup ()
- {
- } /*DoPageSetup ()*/
-
- /*----------*/
- static void DoPrint (void);
- static void DoPrint ()
- {
- } /*DoPrint ()*/
-
- /*----------*/
- void DoFile (itemNr)
- short itemNr;
- {
- errorFlag = false;
-
- switch (itemNr) {
- case FileNew:
- DoNew ();
- break;
- case FileOpen:
- DoOpen ();
- break;
- case FileClose:
- DoClose ();
- break;
- case FileSave:
- DoSave ();
- break;
- case FileSaveAs:
- DoSaveAs ();
- break;
- case FileRevert:
- DoRevert ();
- break;
- case FilePageSetup:
- DoPageSetup ();
- break;
- case FilePrint:
- DoPrint ();
- break;
- case FileQuit:
- DoQuit ();
- break;
- } /*switch*/
- } /*DoFile*/
-
- /* FileM */